<?php
namespace app\common\model;
use think\Model;
class Files extends Model
{  
	//获取新闻详细页
	public function getNewsShow($id){
		$this->where('id','=',$id)->setInc('hits',1); //访问+1
		$news = $this->alias("a")
				->join("type b","a.cid=b.id","left")
			     ->join("type c","a.sid=c.id","left")
			     ->field("a.*,b.name as cname,c.name as sname")
				->find($id);
		return $news;
	}

	//获取移动端列表
	public function getMobileList(){		
	    $cid = intval(input("post.cid"));
	    $sid = intval(input("post.sid"));
	    $title = input("post.title");
	    $pageNum = intval(input("post.pageNum"));
	    $pageSize = intval(input("post.pageSize"));
	    if($pageNum<=1){
	         $star = 0;
	    }else{
	         $star = $pageSize*($pageNum-1);
	    }
	     $map = []; 
	     if($cid) $map["a.cid"] = array('=',$cid);
	     if($sid) $map["a.sid"] = array('=',$sid);
	     if($title) $map['a.name'] = array('like',"%$title%");
	     $order = "a.vieworder desc,a.addtime desc";
	     //计算当前总页数
	     $total = $this->alias("a")->where($map)->count();
	     $totalPage = ceil($total/$pageSize);
	     //判断是否又下一页
	     $hasNext = true;
	     if($pageNum==$totalPage) $hasNext = false;
	     //读取信息
	     $data=[];
	     $_data =  $this->alias("a")
	     ->join("type b","a.cid=b.id","left")
	     ->join("type c","a.sid=c.id","left")
	     ->field("a.*,b.name as cname,c.name as sname")
	     ->where($map)
	     ->order($order)
	     ->limit($star,$pageSize)
	     ->select();         
	      $data = [];
	      if($_data){
	         //重组数据结构
	         foreach ($_data as $k => $v) {
	            $data[$k]['name'] = $v["name"].".pdf";
	            $data[$k]['cname'] = $v["cname"];
	            $data[$k]['sname'] = $v["sname"];
	            $data[$k]['href'] = url('mobile/index/show',array('id'=>$v['id']));
	            $data[$k]['addtime'] = date('Y年m月d日',$v["addtime"]);
	            $data[$k]['filesize'] = ceil($v['filesize']/1024);               
	         }                
        }
        return json(["code"=>200,"data"=>$data,"total"=>$total,"totalPage"=>$totalPage,"hasNext"=>$hasNext]);

	}

}


